{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Nice Pairs in an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #math #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #数学 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countNicePairs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计一个数组中好对子的数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个数组 <code>nums</code> ，数组中只包含非负整数。定义 <code>rev(x)</code> 的值为将整数 <code>x</code> 各个数字位反转得到的结果。比方说 <code>rev(123) = 321</code> ， <code>rev(120) = 21</code> 。我们称满足下面条件的下标对 <code>(i, j)</code> 是 <strong>好的</strong> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= i &lt; j &lt; nums.length</code></li>\n",
    "\t<li><code>nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>请你返回好下标对的数目。由于结果可能会很大，请将结果对 <code>10<sup>9</sup> + 7</code> <b>取余</b> 后返回。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [42,11,1,97]\n",
    "<b>输出：</b>2\n",
    "<b>解释：</b>两个坐标对为：\n",
    " - (0,3)：42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121 。\n",
    " - (1,2)：11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [13,10,35,24,76]\n",
    "<b>输出：</b>4\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-nice-pairs-in-an-array](https://leetcode.cn/problems/count-nice-pairs-in-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-nice-pairs-in-an-array](https://leetcode.cn/problems/count-nice-pairs-in-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[42,11,1,97]', '[13,10,35,24,76]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        diff_list = []\n",
    "        for i in range(len(nums)):\n",
    "            diff_list.append(nums[i] -int(str(nums[i])[::-1]))\n",
    "        diff_list.sort()\n",
    "        i =0\n",
    "        while i <len(diff_list)-1:\n",
    "            count = 1\n",
    "            while i < len(diff_list) - 1 and diff_list[i] == diff_list[i + 1]:\n",
    "                count += 1\n",
    "                i += 1\n",
    "            ans += count*(count-1)/2\n",
    "            i+=1\n",
    "           \n",
    "        return int(ans%(10**9+7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        mod = 10**9 + 7\n",
    "        ans = 0\n",
    "        pre = defaultdict(int)\n",
    "        for num in nums:\n",
    "            cur = num - int(str(num)[::-1])\n",
    "            ans += pre[cur]\n",
    "            pre[cur] += 1\n",
    "        return ans % mod\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        res=0\n",
    "        cnt=Counter()\n",
    "        for i in nums:\n",
    "            j=int(str(i)[::-1])\n",
    "            res+=cnt[i-j]\n",
    "            cnt[i-j]+=1\n",
    "        return res%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        # nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]) -->>转化\n",
    "        # nums[i] - rev(nums[i]) = nums[j] - rev(nums[j])\n",
    "        t = Counter()\n",
    "        res = 0\n",
    "        for x in nums:\n",
    "            tm = x-int(str(x)[::-1])\n",
    "            res += t[tm]\n",
    "            t[tm] += 1\n",
    "        return res %(10**9+7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        cnt = Counter()\n",
    "        for i in nums:\n",
    "            j = int(str(i)[::-1])\n",
    "            res += cnt[i - j]\n",
    "            cnt[i - j] += 1\n",
    "        return res % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        cnt = Counter()\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            d = x - int(str(x)[::-1])\n",
    "            ans += cnt[d]\n",
    "            cnt[d] += 1\n",
    "        return ans % int(1e9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        # def rev (nu:int)->int:\n",
    "        #     revnu = 0\n",
    "        #     while nu!=0:\n",
    "        #         revnu = revnu*10 + nu%10\n",
    "        #         nu //= 10\n",
    "        #     return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            return int(str(nu)[::-1])\n",
    "            # nustr = str(nu)\n",
    "            # nustr = nustr[::-1]\n",
    "            # return int(nustr)\n",
    "            \n",
    "        # revnums = [ i - rev1(i) for i in nums]\n",
    "        # for i in revnums:\n",
    "        #     dmap[i] = dmap[i]+1\n",
    "        for i in nums:\n",
    "            te = i - rev1(i)\n",
    "            if dmap[te] >= 1:\n",
    "                re = (re + dmap[te])\n",
    "            dmap[te] = dmap[te] + 1\n",
    "        # for k in dmap:\n",
    "        #     v = dmap[k]\n",
    "        #     if v>1:\n",
    "        #         re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re%MOD\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        cnt = collections.Counter()\n",
    "        for num in nums:\n",
    "            rev = int(str(num)[::-1])\n",
    "            ans += cnt[num - rev]\n",
    "            cnt[num - rev] += 1\n",
    "        return ans % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        # nums[i] - rev(nums[i]) == nums[j] - rev(nums[j])\n",
    "        res = 0\n",
    "\n",
    "        def rev(i: int) -> int:\n",
    "            res = int(str(i)[::-1])\n",
    "            return res\n",
    "        cnt = Counter()\n",
    "        for num in nums:\n",
    "            revnum = num - rev(num)\n",
    "            res += cnt[revnum]\n",
    "            cnt[revnum] += 1\n",
    "        return res % (10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        def rev (nu:int)->int:\n",
    "            revnu = 0\n",
    "            while nu!=0:\n",
    "                revnu = revnu*10 + nu%10\n",
    "                nu //= 10\n",
    "            return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            nustr = str(nu)\n",
    "            nustr = nustr[::-1]\n",
    "            return int(nustr)\n",
    "            \n",
    "        # revnums = [ i - rev1(i) for i in nums]\n",
    "        # for i in revnums:\n",
    "        #     dmap[i] = dmap[i]+1\n",
    "        for i in nums:\n",
    "            te = i - rev(i)\n",
    "            dmap[te] = dmap[te] + 1\n",
    "        for k in dmap:\n",
    "            v = dmap[k]\n",
    "            if v>1:\n",
    "                re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        dict1, num = {}, 0\n",
    "        for i in nums:\n",
    "            value = i - int(str(i)[::-1])\n",
    "            num += dict1.get(value, 0)\n",
    "            dict1[value] = dict1.get(value, 0) + 1\n",
    "        return num % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(x):\n",
    "            y = 0\n",
    "            while x>0:\n",
    "                y = y*10+x%10\n",
    "                x = x//10\n",
    "            return y\n",
    "        \n",
    "        sums = Counter(rev(x)-x for x in nums)\n",
    "\n",
    "        mod = 10**9 + 7\n",
    "        return sum(v * (v - 1) // 2 for v in sums.values()) % mod\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(x: int) -> int:\n",
    "            x = str(x)\n",
    "            return int(x[::-1])\n",
    "\n",
    "        d = {}\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            ans += d.get(x - rev(x), 0)\n",
    "            d[x - rev(x)] = d.get(x - rev(x), 0) + 1\n",
    "        return ans % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        hashmap = {}\n",
    "        res = 0\n",
    "        for i in range(len(nums)):\n",
    "            index = nums[i] - int(str(nums[i])[::-1])\n",
    "            if index in hashmap:\n",
    "                res += hashmap[index]\n",
    "                hashmap[index] += 1\n",
    "            else:\n",
    "                hashmap[index] = 1\n",
    "        return res%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        # def rev (nu:int)->int:\n",
    "        #     revnu = 0\n",
    "        #     while nu!=0:\n",
    "        #         revnu = revnu*10 + nu%10\n",
    "        #         nu //= 10\n",
    "        #     return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            return int(str(nu)[::-1])\n",
    "            # nustr = str(nu)\n",
    "            # nustr = nustr[::-1]\n",
    "            # return int(nustr)\n",
    "            \n",
    "        # revnums = [ i - rev1(i) for i in nums]\n",
    "        # for i in revnums:\n",
    "        #     dmap[i] = dmap[i]+1\n",
    "        for i in nums:\n",
    "            te = i - rev1(i)\n",
    "            dmap[te] = dmap[te] + 1\n",
    "        for k in dmap:\n",
    "            v = dmap[k]\n",
    "            if v>1:\n",
    "                # re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "                re = (re+(v*(v-1)//2))\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re%MOD\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self) -> None:\n",
    "        self.MODULO = 10 ** 9 + 7\n",
    "\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        retVal = 0\n",
    "\n",
    "        # /* nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])\n",
    "        #  * =>  nums[i] - rev(nums[i]) == nums[j] - rev(nums[j])\n",
    "        #  */\n",
    "        hashTable = defaultdict(int)\n",
    "        for num in nums:\n",
    "            index = num - int(str(num)[::-1])\n",
    "            retVal += hashTable[index]\n",
    "            retVal %= self.MODULO\n",
    "            hashTable[index] += 1\n",
    "\n",
    "        return retVal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        d, result, MOD = defaultdict(int), 0, 10 ** 9 + 7\n",
    "        for num in nums:\n",
    "            result += d[num - int(str(num)[::-1])]\n",
    "            d[num - int(str(num)[::-1])] += 1\n",
    "        return result % MOD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(x):\n",
    "            y = 0\n",
    "            while x: \n",
    "                y = y * 10 + x % 10\n",
    "                x //= 10\n",
    "            return y\n",
    "        cnt = Counter(x-rev(x) for x in nums)\n",
    "        mod = 10 ** 9 + 7\n",
    "        return sum(v * (v-1) // 2 for v in cnt.values()) % mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        # def rev (nu:int)->int:\n",
    "        #     revnu = 0\n",
    "        #     while nu!=0:\n",
    "        #         revnu = revnu*10 + nu%10\n",
    "        #         nu //= 10\n",
    "        #     return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            return int(str(nu)[::-1])\n",
    "            # nustr = str(nu)\n",
    "            # nustr = nustr[::-1]\n",
    "            # return int(nustr)\n",
    "            \n",
    "        # revnums = [ i - rev1(i) for i in nums]\n",
    "        # for i in revnums:\n",
    "        #     dmap[i] = dmap[i]+1\n",
    "        for i in nums:\n",
    "            te = i - rev1(i)\n",
    "            dmap[te] = dmap[te] + 1\n",
    "        for k in dmap:\n",
    "            v = dmap[k]\n",
    "            if v>1:\n",
    "                re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\r\n",
    "        cnt = collections.Counter()\r\n",
    "        ans = 0\r\n",
    "        for n in nums:\r\n",
    "            rev = int(str(n)[::-1])\r\n",
    "            ans += cnt[n - rev]\r\n",
    "            cnt[n - rev] += 1\r\n",
    "        return ans % (10**9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "\n",
    "        count = 0\n",
    "        cache_dict = {}\n",
    "        \n",
    "        # nums[i] - rev(nums[i]) = nums[j] - rev(nums[j])\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            # for j in range(i+1, len(nums)):\n",
    "                target = nums[i] - int(''.join([i for i in str(nums[i])][::-1]))\n",
    "                if target in cache_dict.keys():\n",
    "                    count += cache_dict[target]\n",
    "                    cache_dict[target] += 1\n",
    "                else:\n",
    "                    cache_dict[target] = 1\n",
    "        \n",
    "        return count % (10**9 + 7)\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        from collections import defaultdict\n",
    "        counter = defaultdict(int)\n",
    "\n",
    "        res = 0\n",
    "        for i in nums:\n",
    "            j = int(str(i)[::-1])\n",
    "            res += counter[i-j]\n",
    "            counter[i-j] += 1\n",
    "        return res%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        cnt = Counter()\n",
    "        for i in nums:\n",
    "            j = int(str(i)[::-1])\n",
    "            res += cnt[i - j]\n",
    "            cnt[i - j] += 1\n",
    "        return res % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(a):\n",
    "            ra = 0\n",
    "            while a > 0:\n",
    "                ra = ra * 10 + a % 10\n",
    "                a = a // 10\n",
    "            return ra\n",
    "\n",
    "        m = {}\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            k = num - rev(num)\n",
    "            if not k in m:\n",
    "                m[k] = 1\n",
    "            else:\n",
    "                ans += m[k]\n",
    "                m[k] += 1\n",
    "\n",
    "        return ans % (10**9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        hashmap= dict()\n",
    "        visit = set()\n",
    "        res = 0\n",
    "        for num in nums:\n",
    "            temp = num - self.rev(num)\n",
    "            if temp in hashmap:\n",
    "                hashmap[temp] += 1\n",
    "            else:\n",
    "                hashmap[temp] = 1\n",
    "        for key in hashmap.keys():\n",
    "            res = (res + self.countRes(hashmap[key])) % (10**9 + 7)\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "    def rev(self, num):\n",
    "        res = 0\n",
    "        a = num\n",
    "        b = 0\n",
    "        while a:\n",
    "            b = a % 10\n",
    "            a = a // 10\n",
    "            res = 10 * res + b\n",
    "        return res\n",
    "\n",
    "    def countRes(self, num):\n",
    "        return num * (num-1) // 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        # def rev (nu:int)->int:\n",
    "        #     revnu = 0\n",
    "        #     while nu!=0:\n",
    "        #         revnu = revnu*10 + nu%10\n",
    "        #         nu //= 10\n",
    "        #     return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            nustr = str(nu)\n",
    "            nustr = nustr[::-1]\n",
    "            return int(nustr)\n",
    "            \n",
    "        # revnums = [ i - rev1(i) for i in nums]\n",
    "        # for i in revnums:\n",
    "        #     dmap[i] = dmap[i]+1\n",
    "        for i in nums:\n",
    "            te = i - rev1(i)\n",
    "            dmap[te] = dmap[te] + 1\n",
    "        for k in dmap:\n",
    "            v = dmap[k]\n",
    "            if v>1:\n",
    "                re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        MOD = 10**9 + 7\n",
    "        ans = 0\n",
    "        s = defaultdict(int)\n",
    "        for i in nums:\n",
    "            t = i - int(str(i)[::-1])\n",
    "            ans += s[t]\n",
    "            s[t] += 1\n",
    "        return ans % MOD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    \"\"\"\r\n",
    "        0 <= i < j < nums.length\r\n",
    "        nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])\r\n",
    "        => nums[i] - rev(nums[i]) == nums[j] - rev(nums[j])\r\n",
    "    \"\"\"\r\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\r\n",
    "        MOD = 10 ** 9 + 7\r\n",
    "        ans = 0\r\n",
    "        cnt = Counter()\r\n",
    "        for x in nums:\r\n",
    "            rev_x = int(str(x)[::-1])\r\n",
    "            ans += cnt[x - rev_x]\r\n",
    "            cnt[x - rev_x] += 1\r\n",
    "        return ans % MOD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        cnt = Counter()\n",
    "        for i in nums:\n",
    "            j = int(str(i)[::-1])\n",
    "            res += cnt[i - j]\n",
    "            cnt[i - j] += 1\n",
    "        return res % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "\n",
    "        data_dict = {}\n",
    "        for num in nums:\n",
    "            \n",
    "            t = num - int(str(num)[::-1])\n",
    "            if t in data_dict:\n",
    "                data_dict[t] +=1\n",
    "\n",
    "            else:\n",
    "                data_dict[t] = 1\n",
    "        sum_flag = list(range(max(data_dict.values())))\n",
    "        for i in range(1, len(sum_flag)):\n",
    "            sum_flag[i]+=sum_flag[i-1]\n",
    "        # print(sum_flag)\n",
    "        ans = 0\n",
    "        for v in data_dict.values():\n",
    "            ans += sum_flag[v-1]\n",
    "        return ans%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        # def rev (nu:int)->int:\n",
    "        #     revnu = 0\n",
    "        #     while nu!=0:\n",
    "        #         revnu = revnu*10 + nu%10\n",
    "        #         nu //= 10\n",
    "        #     return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            return int(str(nu)[::-1])\n",
    "            # nustr = str(nu)\n",
    "            # nustr = nustr[::-1]\n",
    "            # return int(nustr)\n",
    "            \n",
    "        # revnums = [ i - rev1(i) for i in nums]\n",
    "        # for i in revnums:\n",
    "        #     dmap[i] = dmap[i]+1\n",
    "        for i in nums:\n",
    "            te = i - rev1(i)\n",
    "            dmap[te] = dmap[te] + 1\n",
    "        for k in dmap:\n",
    "            v = dmap[k]\n",
    "            if v>1:\n",
    "                re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "                # re = (re+(v*(v-1)//2))\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re%MOD\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(x):\n",
    "            y = 0\n",
    "            while x:\n",
    "                y = y * 10 + x % 10\n",
    "                x //= 10\n",
    "            return y\n",
    "\n",
    "        cnt = Counter(x - rev(x) for x in nums)\n",
    "        mod = 10**9 + 7\n",
    "        return sum(v * (v - 1) // 2 for v in cnt.values()) % mod\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        # A + rev(b) = rev(a) + b\n",
    "        # a - rev(a) = b - rev(b)\n",
    "        values = defaultdict(int)\n",
    "        for x in nums:\n",
    "            revX = int(str(x)[::-1])\n",
    "            values[x-revX] += 1\n",
    "        \n",
    "        res = 0\n",
    "        for k, v in values.items():\n",
    "            if v > 1:\n",
    "                res += v * (v-1) // 2\n",
    "\n",
    "        return res % (10**9 + 7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        # def rev (nu:int)->int:\n",
    "        #     revnu = 0\n",
    "        #     while nu!=0:\n",
    "        #         revnu = revnu*10 + nu%10\n",
    "        #         nu //= 10\n",
    "        #     return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            return int(str(nu)[::-1])\n",
    "            # nustr = str(nu)\n",
    "            # nustr = nustr[::-1]\n",
    "            # return int(nustr)\n",
    "            \n",
    "        # revnums = [ i - rev1(i) for i in nums]\n",
    "        # for i in revnums:\n",
    "        #     dmap[i] = dmap[i]+1\n",
    "        for i in nums:\n",
    "            te = i - rev1(i)\n",
    "            if dmap[te] >= 1:\n",
    "                re = (re + dmap[te])%MOD\n",
    "            dmap[te] = dmap[te] + 1\n",
    "        # for k in dmap:\n",
    "        #     v = dmap[k]\n",
    "        #     if v>1:\n",
    "        #         re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        cnt = Counter()\n",
    "        ret = 0\n",
    "        M = 10 ** 9 + 7\n",
    "        for x in nums:\n",
    "            s = str(x)\n",
    "            cnt[x - int(s[::-1])] += 1\n",
    "        for k, v in cnt.items():\n",
    "            ret += v * (v - 1) // 2\n",
    "            ret %= M\n",
    "        return ret\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(x):\n",
    "            y = 0\n",
    "            while x:\n",
    "                y = y*10 + x%10\n",
    "                x //= 10\n",
    "\n",
    "            return y\n",
    "\n",
    "        cnt = Counter(x-rev(x) for x in nums)\n",
    "        mod = 10**9+7\n",
    "        return sum(v * (v-1)//2 for v in cnt.values())%mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def rev(num):\n",
    "    ret = 0\n",
    "    while num:\n",
    "        ret = 10 * ret + num % 10\n",
    "        num //= 10\n",
    "    return ret\n",
    "M = 10 ** 9 + 7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        group_dict = collections.defaultdict(int)\n",
    "        for num in nums:\n",
    "            d = num - rev(num)\n",
    "            group_dict[d] += 1\n",
    "        return sum(val * (val - 1)//2 for val in group_dict.values()) % M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        def rev (nu:int)->int:\n",
    "            revnu = 0\n",
    "            while nu!=0:\n",
    "                revnu = revnu*10 + nu%10\n",
    "                nu //= 10\n",
    "            return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            nustr = str(nu)\n",
    "            nustr = nustr[::-1]\n",
    "            return int(nustr)\n",
    "            \n",
    "        # revnums = [ i - rev1(i) for i in nums]\n",
    "        # for i in revnums:\n",
    "        #     dmap[i] = dmap[i]+1\n",
    "        for i in nums:\n",
    "            te = i - rev1(i)\n",
    "            dmap[te] = dmap[te] + 1\n",
    "        for k in dmap:\n",
    "            v = dmap[k]\n",
    "            if v>1:\n",
    "                re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        d = {}\n",
    "        res = 0\n",
    "        mod = 10**9+7\n",
    "        for num in nums:\n",
    "            rev = int(str(num)[::-1])\n",
    "            k = num - rev\n",
    "            if k in d:\n",
    "                res = (res+d[k])%mod\n",
    "                d[k] += 1\n",
    "            else:\n",
    "                d[k] = 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        remain = 1000000007\n",
    "        ans = 0\n",
    "        used = {}\n",
    "        \n",
    "        for num in nums:\n",
    "            rev = int(str(num)[::-1])\n",
    "            diff = num - rev\n",
    "            if diff not in used: used[diff] = 0\n",
    "            used[diff] += 1\n",
    "        \n",
    "        for diff in used:\n",
    "            number = used[diff]\n",
    "            ans += number*(number - 1) // 2\n",
    "            ans %= remain\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        mod = 10 ** 9 + 7\n",
    "        def rev(x):\n",
    "            a = 0\n",
    "            while x > 0:\n",
    "                t = x % 10\n",
    "                x //= 10\n",
    "                a = a * 10 + t\n",
    "            return a\n",
    "        cnt = Counter()\n",
    "        ans = 0\n",
    "        for i in range(len(nums)):\n",
    "            x = nums[i] - rev(nums[i])\n",
    "            if x in cnt:\n",
    "                ans += cnt[x]\n",
    "                cnt[x] += 1\n",
    "                \n",
    "            else:\n",
    "                cnt[x] = 1\n",
    "        return ans % mod\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        dic={}\n",
    "        n=len(nums)\n",
    "        r=0\n",
    "        for i in range(n):\n",
    "            revc=int(str(nums[i])[::-1])-nums[i]\n",
    "            if revc in dic:\n",
    "                r+=dic[revc]\n",
    "            if revc in dic:\n",
    "                dic[revc]+=1\n",
    "            else:\n",
    "                dic[revc]=1\n",
    "        return r%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "\n",
    "        data_dict = {}\n",
    "        for num in nums:\n",
    "            \n",
    "            t = num - int(str(num)[::-1])\n",
    "            if t in data_dict:\n",
    "                data_dict[t] +=1\n",
    "\n",
    "            else:\n",
    "                data_dict[t] = 1\n",
    "        sum_flag = list(range(max(data_dict.values())))\n",
    "        for i in range(1, len(sum_flag)):\n",
    "            sum_flag[i]+=sum_flag[i-1]\n",
    "        # print(sum_flag)\n",
    "        ans = 0\n",
    "        for v in data_dict.values():\n",
    "            ans += sum_flag[v-1]\n",
    "        return ans%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "      def rev(n):\n",
    "        ret = 0\n",
    "        while n > 0:\n",
    "          ret = ret * 10 + n % 10\n",
    "          n //= 10\n",
    "        return ret\n",
    "\n",
    "      d = collections.defaultdict(int)\n",
    "      ret = 0\n",
    "      for num in nums:\n",
    "        rnum = rev(num)\n",
    "        ret += d[num - rnum]\n",
    "        d[num - rnum] += 1\n",
    "      \n",
    "      return ret % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        dic={}\n",
    "        def reverse(i):\n",
    "            res=0\n",
    "            while i>0:\n",
    "                res=res*10+i%10 \n",
    "                i=i//10 \n",
    "            return res\n",
    "        for i in nums:\n",
    "            f=reverse(i)  \n",
    "            t=i-f  \n",
    "            if t in dic.keys():\n",
    "                dic[t]+=1\n",
    "            else:\n",
    "                dic[t]=1 \n",
    "        res=0 \n",
    "        print(dic)\n",
    "        for j in dic.keys():\n",
    "            if dic[j]>=2: \n",
    "                res+=dic[j]*(dic[j]-1)//2\n",
    "                res=res%(10**9+7)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\r\n",
    "        cnt = Counter()\r\n",
    "        for x in nums:\r\n",
    "            cnt[x - int(str(x)[::-1])] += 1\r\n",
    "        res = 0\r\n",
    "        for k, v in cnt.items():\r\n",
    "            res = (res + comb(v, 2)) % (10 ** 9 + 7)\r\n",
    "        return res \r\n",
    "\r\n",
    "        \r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        q=Counter()\n",
    "        ans=0\n",
    "        for x in nums:\n",
    "          c=int(str(x)[::-1])\n",
    "          print(c)\n",
    "          if x-c in q:\n",
    "            ans+=q[x-c]\n",
    "          q[x-c]+=1\n",
    "        return ans%(10**9+7)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        def rev(num: int) -> int:\n",
    "            print(num)\n",
    "            rev = 0\n",
    "            while num > 0:\n",
    "                digit = num % 10\n",
    "                num = num // 10\n",
    "                rev = rev * 10 + digit\n",
    "            return rev\n",
    "        \n",
    "        ans = 0\n",
    "        lookup = {}\n",
    "        for num in nums:\n",
    "            rev_diff = num - rev(num)\n",
    "            if rev_diff in lookup:\n",
    "                ans = (ans + lookup[rev_diff]) % MOD\n",
    "                lookup[rev_diff] += 1\n",
    "            else:\n",
    "                lookup[rev_diff] = 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        cnt = Counter()\n",
    "        for num in nums:\n",
    "            rev_num = int(str(num)[::-1])\n",
    "            res += cnt[num-rev_num]\n",
    "            cnt[num-rev_num] += 1\n",
    "        return res%(10**9+7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "import math\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def change(n):\n",
    "            res = 0\n",
    "            tmp = n\n",
    "            while n > 0:\n",
    "                n, r = divmod(n, 10)\n",
    "                res = res * 10 + r\n",
    "            return res - tmp\n",
    "\n",
    "        c_nums = [change(x) for x in nums]\n",
    "        ans = 0\n",
    "        cnt = collections.Counter(c_nums)\n",
    "        for v in cnt.values():\n",
    "            ans += math.comb(v, 2)\n",
    "        return ans % (10 ** 9 + 7)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "import math\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def change(n):\n",
    "            res = 0\n",
    "            tmp = n\n",
    "            while n > 0:\n",
    "                n, r = divmod(n, 10)\n",
    "                res = res * 10 + r\n",
    "            return res - tmp\n",
    "\n",
    "        c_nums = [change(x) for x in nums]\n",
    "        ans = 0\n",
    "        cnt = collections.Counter(c_nums)\n",
    "        for v in cnt.values():\n",
    "            ans += math.comb(v, 2)\n",
    "        return ans % (10 ** 9 + 7)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(x):\n",
    "            digits = []\n",
    "            while x>0:\n",
    "                digit = x%10\n",
    "                digits.append(digit)\n",
    "                x = x//10\n",
    "            s = 0\n",
    "            l = len(digits)-1\n",
    "            for idx, d in enumerate(digits):\n",
    "                s+=d* 10**(l-idx)\n",
    "            return int(s)\n",
    "        \n",
    "        sums = [rev(x)-x for x in nums]\n",
    "        sums_set = set(sums)\n",
    "\n",
    "        cnt = 0\n",
    "\n",
    "        for i in sums_set:\n",
    "            d_cnt = sums.count(i)\n",
    "            cnt+=(d_cnt*(d_cnt-1)/2)\n",
    "        \n",
    "        return int(cnt%(10**9+7))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        cnt = Counter(list(map(lambda x: x - int(str(x)[::-1]), nums)))\n",
    "        return sum([(cnt[i] * (cnt[i] - 1) // 2) for i in cnt if cnt[i] > 1]) % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(x):\n",
    "            return int(str(x)[::-1])\n",
    "        \n",
    "        diff = [x - rev(x) for x in nums]\n",
    "        \n",
    "        cnt = Counter(diff)\n",
    "        ans = 0\n",
    "        mod = 10**9 + 7\n",
    "        for k, v in cnt.items():\n",
    "            if v > 0:\n",
    "                ans += v * (v - 1) // 2\n",
    "                ans %= mod\n",
    "        return ans            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "import math\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def change(n):\n",
    "            res = 0\n",
    "            tmp = n\n",
    "            while n > 0:\n",
    "                n, r = divmod(n, 10)\n",
    "                res = res * 10 + r\n",
    "            return res - tmp\n",
    "\n",
    "        c_nums = [change(x) for x in nums]\n",
    "        ans = 0\n",
    "        cnt = collections.Counter(c_nums)\n",
    "        for v in cnt.values():\n",
    "            ans += (v*(v-1))//2\n",
    "        return ans % (10 ** 9 + 7)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rev_diff(self,num:int):\n",
    "        res_num = list(str(num))\n",
    "        res_num.reverse()\n",
    "        res_num = int(\"\".join(res_num))\n",
    "        return num - res_num\n",
    "\n",
    "\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        nums_diff = list(map(self.rev_diff, nums))\n",
    "        maps = collections.defaultdict(int)\n",
    "        for i in nums_diff:\n",
    "            maps[i] += 1\n",
    "        res = 0\n",
    "        for i in maps:\n",
    "            res += maps[i] * (maps[i]-1) // 2\n",
    "        return res % (10**9 +7)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        def rev (nu:int)->int:\n",
    "            revnu = 0\n",
    "            while nu!=0:\n",
    "                revnu = revnu*10 + nu%10\n",
    "                nu //= 10\n",
    "            return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            nustr = str(nu)\n",
    "            nustr = nustr[::-1]\n",
    "            return int(nustr)\n",
    "            \n",
    "        revnums = [ i - rev1(i) for i in nums]\n",
    "        for i in revnums:\n",
    "            dmap[i] = dmap[i]+1\n",
    "        for k in dmap:\n",
    "            v = dmap[k]\n",
    "            if v>1:\n",
    "                re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "MOD = 10**9+7\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        re = 0\n",
    "        dmap = defaultdict(int)\n",
    "        def rev (nu:int)->int:\n",
    "            revnu = 0\n",
    "            while nu!=0:\n",
    "                revnu = revnu*10 + nu%10\n",
    "                nu //= 10\n",
    "            return revnu\n",
    "            \n",
    "        def rev1(nu:int)->int:\n",
    "            nustr = str(nu)\n",
    "            nu = nu[::-1]\n",
    "            return int(nu)\n",
    "            \n",
    "        revnums = [ i - rev(i) for i in nums]\n",
    "        for i in revnums:\n",
    "            dmap[i] = dmap[i]+1\n",
    "        for k in dmap:\n",
    "            v = dmap[k]\n",
    "            if v>1:\n",
    "                re = (re+(v * (v-1)//2 % MOD))%MOD\n",
    "        # print(revnums)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(i+1,n):\n",
    "        #         if(nums[i] + revnums[j] == revnums[i]+ nums[j]):\n",
    "        #             nums[i] - revnums[i] == nums[j] - revnums[j]\n",
    "        #             re = (re+1)%MOD\n",
    "        return re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        def rev(x):\n",
    "            y = 0\n",
    "            while x>0:\n",
    "                y = y*10+x%10\n",
    "                x = x//10\n",
    "            return y\n",
    "        \n",
    "        sums = [rev(x)-x for x in nums]\n",
    "        sums_set = set(sums)\n",
    "\n",
    "        cnt = 0\n",
    "\n",
    "        for i in sums_set:\n",
    "            d_cnt = sums.count(i)\n",
    "            cnt+=(d_cnt*(d_cnt-1)/2)\n",
    "        \n",
    "        return int(cnt%(10**9+7))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        return sum([(v * (v - 1) >> 1) for k, v in c.items()]) % (10 ** 9 + 7) if (c := Counter(list(map(lambda x: x - int(str(x)[::-1]), nums)))) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        mod = 10 ** 9 + 7\n",
    "        n = len(nums)\n",
    "        g = [x - int(str(x)[::-1]) for x in nums]\n",
    "        cnt = Counter()\n",
    "        ans = 0\n",
    "        cnt[g[0]] += 1\n",
    "        for x in g[1:]:\n",
    "            ans += cnt[x]\n",
    "            cnt[x] += 1\n",
    "        return ans % mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countNicePairs(self, nums: List[int]) -> int:\n",
    "        M = 10**9+7\n",
    "        mapping = {}\n",
    "        for num in nums:\n",
    "            if(num not in mapping):\n",
    "                mapping[num] = 1\n",
    "            else:\n",
    "                mapping[num] += 1\n",
    "        def rev(x):\n",
    "            return int(str(x)[::-1])\n",
    "        cnt_map = {}\n",
    "        result = 0\n",
    "        for num in mapping:\n",
    "            result += mapping[num]*(mapping[num]-1)//2 % M\n",
    "            num_rev = num - rev(num)\n",
    "            if(num_rev not in cnt_map):\n",
    "                cnt_map[num_rev] = mapping[num]\n",
    "            else:\n",
    "                result += cnt_map[num_rev]*mapping[num] % M\n",
    "                cnt_map[num_rev] += mapping[num]\n",
    "        return result % M\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
